package org.abc.fund.entity.factorEntity;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;
import java.util.List;
/**
 * 因子树节点实体类
 */
@Entity
@Table(name = "factor_tree_nodes")
@Data
@EqualsAndHashCode(callSuper = false)
public class FactorTreeNode {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 节点名称
     */
    @Column(nullable = false, length = 200)
    private String nodeName;

    /**
     * 节点描述
     */
    @Column(columnDefinition = "TEXT")
    private String description;
    /**
     * 节点类型：GROUP(分组), FACTOR(因子)
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private NodeType nodeType;
    /**
     * 父节点ID
     */
    @Column(name = "parents_id")
    private Long parentId;
    /**
     * 树ID
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tree_id", nullable = false)
    @JsonBackReference("tree-node")
    private FactorTree factorTree;

    /**
     * 关联的因子（当节点类型为FACTOR时）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "factor_id")
    @JsonBackReference
    private Factor factor;

    /**
     * 排序
     */
    @Column
    private Integer sortOrder = 0;

    /**
     * 层级
     */
    @Column(name="t_level")
    private Integer level = 0;

    /**
     * 路径（用于快速查询）
     */
    @Column(length = 500)
    private String path;

    /**
     * 是否启用
     */
    @Column(nullable = false)
    private Boolean enabled = true;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(nullable = false)
    private LocalDateTime updateTime;

    /**
     * 创建人
     */
    @Column(length = 100)
    private String createdBy;

    /**
     * 更新人
     */
    @Column(length = 100)
    private String updatedBy;

    /**
     * 父节点
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    @JsonBackReference("parent-children")
    private FactorTreeNode parent;

    /**
     * 子节点
     */
    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    @JsonManagedReference("parent-children")
    private List<FactorTreeNode> children;

    /**
     * 节点类型枚举
     */
    public enum NodeType {
        BASIC("基础因子"),
        DERIVED("衍生因子"),
        STYLE("风格因子"),
        COMBINATION("组合因子");
        private final String description;

        NodeType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    // ========== 补全getter/setter ==========
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getNodeName() { return nodeName; }
    public void setNodeName(String nodeName) { this.nodeName = nodeName; }
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }
    public NodeType getNodeType() { return nodeType; }
    public void setNodeType(NodeType nodeType) { this.nodeType = nodeType; }
    public Long getParentId() { return parentId; }
    public void setParentId(Long parentId) { this.parentId = parentId; }
    public FactorTree getFactorTree() { return factorTree; }
    public void setFactorTree(FactorTree factorTree) { this.factorTree = factorTree; }
    public Factor getFactor() { return factor; }
    public void setFactor(Factor factor) { this.factor = factor; }
    public Integer getSortOrder() { return sortOrder; }
    public void setSortOrder(Integer sortOrder) { this.sortOrder = sortOrder; }
    public Integer getLevel() { return level; }
    public void setLevel(Integer level) { this.level = level; }
    public String getPath() { return path; }
    public void setPath(String path) { this.path = path; }
    public Boolean getEnabled() { return enabled; }
    public void setEnabled(Boolean enabled) { this.enabled = enabled; }
    public LocalDateTime getCreateTime() { return createTime; }
    public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; }
    public LocalDateTime getUpdateTime() { return updateTime; }
    public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; }
    public String getCreatedBy() { return createdBy; }
    public void setCreatedBy(String createdBy) { this.createdBy = createdBy; }
    public String getUpdatedBy() { return updatedBy; }
    public void setUpdatedBy(String updatedBy) { this.updatedBy = updatedBy; }
    public List<FactorTreeNode> getChildren() { return children; }
    public void setChildren(List<FactorTreeNode> children) { this.children = children; }
}
